package com.xindaoapp.henghua.utils;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import android.text.TextUtils;

public class CommFunc {

	/** 往年*/
	private final static String DATE_OTHER_YEAR = "yyyy-MM-dd HH:mm";
	/** 今年*/
	private final static String DATE_THR_YEAR = "MM-dd HH:mm";
	/** 今天*/
	private final static String DATE_TODAY = "HH:mm";

	private final static long LONG_5_MINUTE = 5 * 60 * 1000;
	private final static long LONG_20_MINUTE = 20 * 60 * 1000;
	private final static long LONG_60_MINUTE = 60 * 60 * 1000;

	public static String getTime(String time) {

		if (TextUtils.isEmpty(time)) {
			return time;
		}
		if (time.length() < 19) {
			return time;
		}

		long lTime = ImDateToLong(time);

		if (!isYear(time)) {
			return getFormatDate(lTime, DATE_OTHER_YEAR);
		} else if (isToday(lTime)) {
			long currentTime = new Date().getTime();
			long differenceTime = currentTime - lTime;
			if (differenceTime < LONG_60_MINUTE) {
				int minute = (int) (differenceTime / 1000 / 60);
				if (minute <= 0) {
					return "刚刚";
				} else {
					return minute + "分钟之前";
				}
			} else {
				return getFormatDate(lTime, DATE_TODAY);
			}
		} else {
			return getFormatDate(lTime, DATE_THR_YEAR);
		}

	}

	/** 判断日期是否为今天 */
	public static boolean isToday(long date) {
		if (date <= 0) {
			return false;
		}
		Calendar cal = Calendar.getInstance(Locale.CHINA);
		int Year = cal.get(Calendar.YEAR);
		int Month = cal.get(Calendar.MONTH);
		int Day = cal.get(Calendar.DAY_OF_MONTH);
		cal.set(Calendar.YEAR, Year);
		cal.set(Calendar.MONTH, Month);
		cal.set(Calendar.DAY_OF_MONTH, Day);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		long today = cal.getTimeInMillis();
		if (date != 0) { // kk:mm:ss
			if (date > today) {
				return true;
			}
		}
		return false;
	}

	/** 判断日期是否为今年 */
	@SuppressWarnings("deprecation")
	public static boolean isYear(String time) {

		Date tDate = strToDateLong(time);
		int tYear = tDate.getYear();

		Date currentDate = new Date();
		int cYear = currentDate.getYear();

		if (tYear == cYear) {
			return true;
		}
		return false;
	}

	/** 
	  * 获取现在时间 
	  *  
	  * @return返回字符串格式 yyyy-MM-dd HH:mm:ss 
	  */
	public static String getStringDate() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	/** 
	  * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss 
	  *  
	  * @param strDate 
	  * @return 
	  */
	public static Date strToDateLong(String strDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ParsePosition pos = new ParsePosition(0);
		Date strtodate = formatter.parse(strDate, pos);
		return strtodate;
	}

	/**
	 * long型转换成日期(存库用)
	 * 
	 * @param date
	 * @return
	 */
	public static String getFormatDate(long date, String format) {
		if (date != 0) { // kk:mm:ss
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			return sdf.format(new Date(date));
		}
		return null;
	}

	/**
	 * 年月日时分格式转时间戳
	 * 
	 * @param dateFormat
	 * @return
	 */
	public static long dateToLong(String dateFormat) {
		if (dateFormat == null) {
			return 0;
		}
		try {
			if (dateFormat.length() > 12) {
				dateFormat = dateFormat.substring(0, 12);
			}
			SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmm"); // 201304251014
			// ss
			df.setLenient(false);
			return df.parse(dateFormat).getTime();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 年月日时分格式转时间戳
	 * 
	 * @param dateFormat
	 * @return
	 */
	public static long ImDateToLong(String dateFormat) {
		if (dateFormat == null) {
			return 0;
		}
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 2013-07-16
			// 15:39:37
			df.setLenient(false);
			return df.parse(dateFormat).getTime();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

}
